﻿using System.Collections.Generic;
using System.IO;
using UnityEngine;
using ZCUtil.Singleton;

namespace ZCTouchDrawBoard
{
    class TouchDrawingServer : Singleton<TouchDrawingServer>
    {
        public List<PaintAttribute> GetPaintList()
        {
            List<PaintAttribute> paintAttributes = new List<PaintAttribute>();
            AddColorPaint(paintAttributes);
            AddMagicPaint(paintAttributes);
            AddStickerPaint(paintAttributes);
            return paintAttributes;
        }

        private void AddColorPaint(List<PaintAttribute> paintAttributes)
        {
            paintAttributes.Add(new PaintAttribute
            {
                PaintType = ePaintType.Color,
                IconPath = "ColorPaint/SnapMask",
                MaterialPath = "ColorPaint/SnapMask",
                IsChangeColor = true,
                PenColor = new Color(0.2f, 0.0f, 0.0f, 0.2f)
            });
            paintAttributes.Add(new PaintAttribute
            {
                PaintType = ePaintType.Color,
                IconPath = "ColorPaint/SnapMask",
                MaterialPath = "ColorPaint/SnapMask",
                IsChangeColor = true,
                PenColor = new Color(0.0f, 0.7f, 0.0f, 0.7f)
            });
        }

        private void AddMagicPaint(List<PaintAttribute> paintAttributes)
        {
            paintAttributes.Add(new PaintAttribute
            {
                PaintType = ePaintType.Magic,
                IconPath = "MagicPaint/Animal",
                MaterialPaths = new string[] { "ColorPaint/SnapMask", "MagicPaint/Animal" },
                IsChangeColor = true,
                PenColor = new Color(1.0f, 1.0f, 1.0f, 1.0f),
                LineLength = 10,
                LineSpace = 20,
            });
            paintAttributes.Add(new PaintAttribute
            {
                PaintType = ePaintType.Magic,
                IconPath = "MagicPaint/Animal",
                MaterialPaths = new string[] { "ColorPaint/SnapMask", "MagicPaint/Animal" },
                IsChangeColor = true,
                PenColor = new Color(0.0f, 1.0f, 1.0f, 1.0f),
                LineLength = 0,
                LineSpace = 10,
            });
        }

        private void AddStickerPaint(List<PaintAttribute> paintAttributes)
        {
            PaintAttribute attribute;
            attribute = new PaintAttribute()
            {
                PaintType = ePaintType.Sticker,
                IsChangeColor = true,
                PenColor = new Color(1.0f, 1.0f, 1.0f, 1.0f),
                LineLength = 0,
                LineSpace = 100,
            };
            SerachStickerPaint(attribute, "StickerPaint/Christmas");
            paintAttributes.Add(attribute);

            attribute = new PaintAttribute()
            {
                PaintType = ePaintType.Sticker,
                IsChangeColor = true,
                PenColor = new Color(0.0f, 1.0f, 1.0f, 1.0f),
                LineLength = 0,
                LineSpace = 100,
            };
            SerachStickerPaint(attribute, "StickerPaint/Planets");
            paintAttributes.Add(attribute);

            attribute = new PaintAttribute()
            {
                PaintType = ePaintType.Sticker,
                IsChangeColor = true,
                PenColor = new Color(0.0f, 1.0f, 1.0f, 1.0f),
                LineLength = 0,
                LineSpace = 100,
            };
            SerachStickerPaint(attribute, "StickerPaint/Winter");
            paintAttributes.Add(attribute);
        }

        private void SerachStickerPaint(PaintAttribute attribute, string path)
        {
            Object[] objects = Resources.LoadAll(path);
            attribute.MaterialPaths = new string[objects.Length];
            for (int i = 0; i < objects.Length; i++)
            {
                attribute.MaterialPaths[i] = path + "/" + objects[i].name;
            }
            attribute.IconPath = path + "/main";
        }
    }

    class TouchDrawingModel
    {
        public void Initialize()
        {

        }

        public void Uninitialize()
        {

        }

        public List<PaintAttribute> UpdatePaintList()
        {
            return TouchDrawingServer.Instance.GetPaintList();
        }
    }
}
